package template

import (
	"io/ioutil"
	"os"
	"strings"
)

type protoKind struct{}

func (p *protoKind) Kind() string {
	return "proto"
}

func (p *protoKind) Gen(rootDir string) error {
	var err error
	rootDir += "/" + *projectRepo + "-proto/" + strings.ReplaceAll(*projectRepo, "-", "_")
	err = os.MkdirAll(rootDir, 0775)
	if err != nil {
		return err
	}
	protoData := projectReplacer.Replace(templateProto)
	protoData = projectProtoReplacer.Replace(protoData)
	protoName := rootDir + "/" + strings.ReplaceAll(*projectRepo, "-", "_") + ".proto"
	err = ioutil.WriteFile(protoName, []byte(protoData), 0666)
	if err != nil {
		return err
	}

	return nil
}

const (
	templateProto = `syntax = "proto3";

package {service_name_proto};

import "gitee.com/kelvins-io/common/proto/google/api/annotations.proto";

option go_package = "proto/{service_name_proto}_proto/{service_name_proto};{service_name_proto}";

service YourService {

	// 在这里写方法的注释
    rpc Service1(Service1Request) returns (Service1Response) {
        option (google.api.http) = {
            put: "/v1/service1"
            body:"*"
        };
    }
   // 在这里写方法的注释
    rpc Service2(Service2Request) returns (Service2Response) {
        option (google.api.http) = {
            delete: "/v1/service2"
        };
    }
   // 在这里写方法的注释
    rpc Service3(Service3Request) returns (Service3Response) {
        option (google.api.http) = {
            get: "/v1/service3"
        };
    }

}

message Service1Request {
	int64 id = 1;
}

message Service1Response {
	CommonResponse common = 1;
	string result = 2;
}

message Service2Request {
	int64 id = 1;
}

message Service2Response {
	CommonResponse common = 1;
	string result = 2;
}

message Service3Request {
	int64 id = 1;
}

message Service3Response {
	CommonResponse common = 1;
	string result = 2;
}

enum RetCode {
    SUCCESS = 0;
    ERROR = 500;
}

message CommonResponse {
    RetCode code = 1;
    string msg = 2;
}`
)
